<template>
  <div class="searchbox">
    <el-input
      v-model.lazy="keyword"
      placeholder="请输入关键词搜索文章"
      style="width:250px"
    ></el-input>
    <transition name="el-fade-in-linear">
      <div v-show="show" class="box u-tri">
        <div class="successbox" v-if="success">
          <h3>搜索结果</h3>
          <vue-scroll :ops="ops">
            <div class="re-box">
              <div
                v-for="(item, index) in articleArray"
                :key="index"
                @click="handlerarticle(item._id)"
                class="s-box"
              >
                <span class="s-title">{{ item.title }}</span>
                <span class="s-category">{{ item.category[0] }}</span>
              </div>
            </div>
          </vue-scroll>
        </div>
        <div v-else class="noresult">无匹配结果,请修改关键词！</div>
      </div>
    </transition>
  </div>
</template>
<script>
import { SearchArticle } from "../api/index"
export default {
  data() {
    return {
      ops: {
        //vuescroll的配置
        vuescroll: {
          sizeStrategy: "percent",
          detectResize: true,
        },
        scrollPanel: {
          scrollingX: false,
        },
        rail: {},
        bar: {
          showDelay: 500,
          onlyShowBarOnScroll: false,
          keepShow: true,
          background: "#b3c0d1",
          opacity: 1,
          hoverStyle: false,
          specifyBorderRadius: false,
          minSize: false,
          size: "6px",
          disable: false,
        },
      },
      keyword: "",
      show: false,
      articleArray: [],
      success: false,
    }
  },
  watch: {
    keyword(newvalue, oldvalue) {
      if (newvalue != "") {
        this.show = true
        this.getarticle({ keyword: this.keyword })
      } else {
        this.show = false
      }
    },
  },
  methods: {
    getarticle(data) {
      SearchArticle(data).then((res) => {
        this.articleArray = []
        this.articleArray.push(...res.data)
        if (this.articleArray.length) {
          this.success = true
        } else {
          this.success = false
        }
      })
    },
    handlerarticle(id) {
      this.keyword = ""
      this.$router.push({ name: "article", query: { id } })
    },
  },
}
</script>
<style lang="scss" scoped>
.searchbox {
  position: relative;
}

.box {
  position: absolute;
  display: inline-block;
  width: 450px;
  height: 250px;
  top: 65px;
  left: 0px;
  border-radius: 2px;
  background-color: white;
  line-height: 20px;
  font-size: 14px;
  z-index: 2147483647;
  box-shadow: 0 5px 8px rgba(10, 16, 20, 0.24), 0 0 8px rgba(10, 16, 20, 0.12);
}
.u-tri:before {
  position: absolute;
  left: 10px;
  top: -20px;
  content: "";
  border-top: 12px solid transparent;
  border-bottom: 12px solid white;
  border-left: 12px solid transparent;
  border-right: 12px solid transparent;
}
.noresult {
  height: 250px;
  line-height: 250px;
  width: 450px;
  text-align: center;
  font-weight: 700;
  color: rgb(97, 97, 100);
}
.successbox {
  padding: 10px 20px;
  .re-box {
    height: 180px;

    .s-box {
      margin: 0px 20px;
      margin-bottom: 10px;
      display: flex;
      justify-content: space-between;
      transition: 0.5s;
      padding: 5px 10px;
      .s-title {
        width: 80%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
    .s-box:hover {
      cursor: pointer;
      color: white;
      background: rgb(64, 158, 255);
    }
  }
}
</style>
